﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuanLyGaRaOTo.PhieuTiepNhan
{
    public partial class Presentation : Form
    {
        private DataTable dtPhieuTiepNhan = new DataTable();

        public Presentation()
        {
            InitializeComponent();
        }

        private void loadPhieuTiepNhan()
        {            
            BUS bus = new BUS();
            dtPhieuTiepNhan = new DataTable();
            dtPhieuTiepNhan = bus.read();
            dgv.DataSource = dtPhieuTiepNhan.DefaultView;
        }

        private DataTable loadHieuXe()
        {
            DataTable dt = new DataTable();
            HieuXe.BUS bus = new HieuXe.BUS();
            dt = bus.read();
            return dt;            
        }

        private void Presentation_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = loadHieuXe();
            colHieuXe.DataSource = dt.DefaultView;
            colHieuXe.ValueMember = "MAHIEUXE";
            colHieuXe.DisplayMember = "TENHIEUXE";      

            DataTable dt1 = dt.Copy();
            cbbHieuXe.DataSource = dt1.DefaultView;
            cbbHieuXe.ValueMember = "MAHIEUXE";
            cbbHieuXe.DisplayMember = "TENHIEUXE";
        
            loadPhieuTiepNhan();
            loadHieuXe();            
        }

        private void cbTenChuXe_CheckedChanged(object sender, EventArgs e)
        {
            txtTenChuXe.Enabled = cbTenChuXe.Checked;
        }

        private void cbBienSo_CheckedChanged(object sender, EventArgs e)
        {
            txtBienSo.Enabled = cbBienSo.Checked;
        }

        private void cbDiaChi_CheckedChanged(object sender, EventArgs e)
        {
            txtDiaChi.Enabled = cbDiaChi.Checked;
        }

        private void cbDienThoai_CheckedChanged(object sender, EventArgs e)
        {
            txtDienThoai.Enabled = cbDienThoai.Checked;
        }

        private void cbHieuXe_CheckedChanged(object sender, EventArgs e)
        {
            cbbHieuXe.Enabled = cbHieuXe.Checked;
        }

        private void cbNgayTiepNhan_CheckedChanged(object sender, EventArgs e)
        {
            dtiNgayTiepNhan.Enabled = cbNgayTiepNhan.Checked;
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            string filter = "";

            if (cbTenChuXe.Checked)
                filter = string.Format("TENCHUXE like '%{0}%'", txtTenChuXe.Text);

            if (cbBienSo.Checked)
            {
                if (filter != "")
                    filter += "and ";

                filter += string.Format("BIENSO like '%{0}%'", txtBienSo.Text);
            }

            if (cbDiaChi.Checked)
            {
                if (filter != "")
                    filter += "and ";

                filter += string.Format("DIACHI like '%{0}%'", txtDiaChi.Text);
            }

            if (cbDienThoai.Checked)
            {
                if (filter != "")
                    filter += "and ";

                filter += string.Format("DIENTHOAI like '%{0}%'", txtDienThoai.Text);
            }

            if (cbHieuXe.Checked)
            {
                if (filter != "")
                    filter += "and ";

                filter += string.Format("HIEUXE = '{0}'", cbbHieuXe.Text);
            }

            if (cbNgayTiepNhan.Checked)            
                if (dtiNgayTiepNhan.Text != "")
                {
                    if (filter != "")
                        filter += "and ";

                    filter += string.Format("NGAYTIEPNHAN = '{0}'", dtiNgayTiepNhan.Text);
                }            

            DataView dv = new DataView(dtPhieuTiepNhan);
            dv.RowFilter = filter;
            dgv.DataSource = dv;
        }        

        private void cbChonTatCa_CheckedChanged(object sender, EventArgs e)
        {
            cbTenChuXe.Checked = cbChonTatCa.Checked;
            cbBienSo.Checked = cbChonTatCa.Checked;
            cbDiaChi.Checked = cbChonTatCa.Checked;
            cbDienThoai.Checked = cbChonTatCa.Checked;
            cbHieuXe.Checked = cbChonTatCa.Checked;
            cbNgayTiepNhan.Checked = cbChonTatCa.Checked;
        }

        private void tsbEdit_Click(object sender, EventArgs e)
        {
            btnStatus(false);
        }

        private void insert(DataRow row)
        {
            DTO dto = new DTO();

            dto.TenChuXe = row["TenChuXe"].ToString();
            dto.BienSo = row["BienSo"].ToString();
            dto.DiaChi = row["DiaChi"].ToString();
            dto.DienThoai = row["DienThoai"].ToString();          
            
            if (row["HieuXe"].ToString() != "")
                dto.HieuXe = Convert.ToInt32(row["HieuXe"]);                

            if (row["NgayTiepNhan"].ToString() != "")
                dto.NgayTiepNhan = Convert.ToDateTime(row["NgayTiepNhan"]);

            BUS bus = new BUS();
            int i = bus.insert(dto);

            if (i == -1)
            {
                MessageBox.Show("Đã xảy ra lỗi trong quá trình thêm dòng dữ liệu mới!!!", "Thêm", MessageBoxButtons.OK, MessageBoxIcon.Error);
                dtPhieuTiepNhan.RejectChanges();
                btnStatus(true);
            }
            else
                row["MaPhieu"] = i;
        }

        private void delete(DataRow row)
        {         
            DTO dto = new DTO();

            dto.MaPhieu = Convert.ToInt32(row["MaPhieu", DataRowVersion.Original]);            

            BUS bus = new BUS();
            if (!bus.delete(dto))
            {
                MessageBox.Show("Đã xảy ra lỗi trong quá trình xóa dòng dữ liệu mới!!!", "Xóa", MessageBoxButtons.OK, MessageBoxIcon.Error);
                dtPhieuTiepNhan.RejectChanges();
                btnStatus(true);
            }            
        }

        private void update(DataRow row)
        {
            DTO dto = new DTO();
            dto.MaPhieu = Convert.ToInt32(row["MaPhieu"]);
            dto.TenChuXe = row["TenChuXe"].ToString();
            dto.BienSo = row["BienSo"].ToString();
            dto.DiaChi = row["DiaChi"].ToString();
            dto.DienThoai = row["DienThoai"].ToString();
            dto.HieuXe = Convert.ToInt32(row["HieuXe"]);

            if (row["NgayTiepNhan"].ToString() != "")
                dto.NgayTiepNhan = Convert.ToDateTime(row["NgayTiepNhan"]);

            BUS bus = new BUS();
            if (!bus.update(dto))
            {
                MessageBox.Show("Đã xảy ra lỗi trong quá trình cập nhật dòng dữ liệu mới!!!", "Cập nhật", MessageBoxButtons.OK, MessageBoxIcon.Error);
                dtPhieuTiepNhan.RejectChanges();
                btnStatus(true);
            }  
        }

        private void tsbSave_Click(object sender, EventArgs e)
        {
            foreach (DataRow row in dtPhieuTiepNhan.Rows)            
                switch (row.RowState)
                {
                    case DataRowState.Added:
                        insert(row);
                        break;

                    case DataRowState.Modified:
                        update(row);
                        break;

                    case DataRowState.Deleted:
                        delete(row);;
                        break;

                    default: break;
                }            

            dtPhieuTiepNhan.AcceptChanges();
            btnStatus(true);
        }

        private void tsbCancel_Click(object sender, EventArgs e)
        {
            dtPhieuTiepNhan.RejectChanges();
            btnStatus(true);
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnStatus(Boolean status)
        {
            try
            {
                grTraCuu.Enabled = status;
                dgv.ReadOnly = status;
                dgv.AllowUserToDeleteRows = !status;

                tsbEdit.Enabled = tsbReset.Enabled = status;
                tsbSave.Enabled = tsbCancel.Enabled = !status;
                dgv.AllowUserToAddRows = !status;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void tsbReset_Click(object sender, EventArgs e)
        {
            dgv.DataSource = dtPhieuTiepNhan.DefaultView;
        }
    }
}
